//
//  MessageVC.m
//  ProjectforHDX
//
//  Created by yinda on 16/8/3.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "MessageVC.h"
#import "MessageCell.h"
#import "MessageModels.h"
#import "ConfirmReceiptVC.h"   //确认收货


@interface MessageVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *dataArray;
}
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MessageVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [dataArray removeAllObjects];
    [self setupDatas];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    dataArray = [[NSMutableArray alloc]init];
    
    [self setupViews];
}

#pragma mark- 数据请求
- (void)setupDatas{
    NSLog(@"======1  %@",dataArray);

    [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
    [AFHttpTool postJsonDatawithUrl:@"messagesOrderBy" parameters:nil success:^(id responseObject) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue] == 1) {
            NSArray *array = [responseObject objectForKey:@"data"];
            NSLog(@"======2  %@",array);
            
            if (array.count < 1) {
                [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView top:120 title:[THMangerTool language:@"HUD.noData"] onClick:^{
                    [self setupDatas];
                }];
            }
            
            for (NSDictionary *dic in array) {
                
                NSLog(@"++++++++%@",dic);
                
                MessageModels *model = [[MessageModels alloc]init];
                model.type = dic[@"type"];
                model.title = dic[@"title"];
                model.content = dic[@"content"];
                model.time = dic[@"time"];
                model.unRead = dic[@"unRead"];
                [dataArray addObject:model];
            }
            
            [self.tableView reloadData];
            
        }else {
            
            [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];

}

#define mark 设置UI
- (void)setupViews {
    
    self.navColor = SYSTEM_COLOR;
    // 我的消息
    self.labnav.text= [UtilCheck language:@"setting.myMessage"];
    
    [self.labnav setFont:NavFontSize];
    [self.labnav setTextColor:[UIColor whiteColor]];
    self.labnav.backgroundColor = [UIColor clearColor];
    
    // view
    self.mBackColor =[UIColor groupTableViewBackgroundColor];
    
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -35, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];

    [self.view addSubview:self.tableView];
    
}


#pragma mark- TalbleView Delegate&DataSource

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSLog(@"%ld",dataArray.count);
    
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
    MessageModels *models = dataArray[indexPath.row];
    
    if ([models.type intValue] == 1) {
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/affirm_receipt.png");
    }else if ([models.type intValue] == 2){
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/failure_sipping.png");
    }else if ([models.type intValue] == 3){
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/cancel_ order.png");
    }else if ([models.type intValue] == 4){
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/else.png");
    }else if ([models.type intValue] == 5){
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/取故障件.png");
    }
    else{
        cell.typeImageView.image = BDFILEIMAGENAEM(@"UserCenter/affirm_receipt.png");
    }
    cell.titleLabel.text = models.title;
    cell.contentLabel.text = models.content;
        
    cell.timeLabel.text = models.time;
    if ([models.unRead intValue] == 0) {
        cell.unReadLabel.hidden = YES;
    }
    cell.unReadLabel.text = [NSString stringWithFormat:@"%@   ",models.unRead];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:NO animated:YES];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[THMangerTool language:@"show.WarmTip"] message:[THMangerTool language:@"MessageVC.deleteMessage"] preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:[THMangerTool language:@"show.cancel"] style:UIAlertActionStyleCancel handler:nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:[THMangerTool language:@"show.confirm"] style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            MessageModels *models = dataArray[indexPath.row];
            [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
            
            NSDictionary *dic = @{@"type":models.type};
            NSLog(@"%@",dic);
            
            [AFHttpTool postJsonDatawithUrl:@"deleteMessagesType" parameters:dic success:^(id responseObject) {
                
                [[BDHUD sharedManager] dismissHUD];

                [dataArray removeAllObjects];

                [self setupDatas];
            } fail:^(id responseObject, NSError *error) {
                
                [[BDHUD sharedManager] dismissHUD];
                
                if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
                }else {
                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
                }
            }];
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

//设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageModels *models = dataArray[indexPath.row];
    ConfirmReceiptVC *vc = [[ConfirmReceiptVC alloc]init];
    vc.type = models.type;
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
